<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="ext.acae2.dms.resource.DataExchangeResource" %>
<%@ taglib uri="http://www.ptc.com/windchill/taglib/fmt" prefix="fmt"%>

<fmt:setBundle basename="ext.acae2.dms.resource.DataExchangeResource" />
<fmt:message var="labelSupplier" key="<%= DataExchangeResource.LABEL_SUPPLIER%>"/>
<fmt:message var="labelDataPackage" key="<%= DataExchangeResource.LABEL_DATA_PACKAGE%>"/>
<fmt:message var="buttonCheck" key="<%= DataExchangeResource.LABEL_CHECK_DATA%>"/>
<fmt:message var="buttonSubmit" key="<%= DataExchangeResource.LABEL_SUBMIT%>"/>
<fmt:message var="labelUploadWarning" key="<%= DataExchangeResource.LABEL_UPLOAD_WARNING%>"/>
<fmt:message var="folderName410" key="<%= DataExchangeResource.FOLDER_NAME_410 %>"/>
<fmt:message var="folderName420" key="<%= DataExchangeResource.FOLDER_NAME_420 %>"/>
<fmt:message var="folderName430" key="<%= DataExchangeResource.FOLDER_NAME_430 %>"/>
<fmt:message var="folderName606" key="<%= DataExchangeResource.FOLDER_NAME_606 %>"/>
<fmt:message var="folderName614" key="<%= DataExchangeResource.FOLDER_NAME_614 %>"/>
<fmt:message var="folderName624" key="<%= DataExchangeResource.FOLDER_NAME_624 %>"/>
<fmt:message var="errorMessageType" key="<%= DataExchangeResource.ERROR_MESSAGE_PACKAGE_TYPE %>"/>
<fmt:message var="labelLog" key="<%= DataExchangeResource.LABEL_LOG%>"/>

<style type="text/css">
table {
    position: absolute;
    left: 100px;
    top: 20px;
}

th {
    text-align:right;
    padding: 6px;
}

td {
    padding: 6px;
}
</style>

<form id="dataSubmit" method="post" action="/Windchill/netmarkets/jsp/ext/acae2/dms/processSubmit.jsp" 
    enctype="multipart/form-data" target="hidden_frame">
<table>
    <tr>
        <th>${labelSupplier}: </th>
        <td>
            <select id="supplier" name="supplier">
                <option value="606">${folderName606}</option>
                <option value="614">${folderName614}</option>
                <option value="624">${folderName624}</option>
<%--                 <option value="410">${folderName410}</option> --%>
<%--                 <option value="420">${folderName420}</option> --%>
<%--                 <option value="430">${folderName430}</option> --%>
            </select>
        </td>
        <td></td>
    </tr>
    <tr>
        <th>${labelDataPackage}: </th>
        <td>
            <input id="uploadFile" name="uploadFile" type="file" onchange="checkFile()"/>
        </td>
        <td>${labelUploadWarning}</td>
    </tr>
    <tr>
        <th></th>
        <td>
        	<input id="checkButton" type="button" value="${buttonCheck}" disabled="disabled" onclick="checkXml()">
            <input id="submitButton" type="submit" value="${buttonSubmit}" disabled="disabled" onclick="submitFile()">
        </td>
        <td></td>
    </tr>
    <tr>
        <th>${labelLog}: </th>
        <td>
            <textarea id="log" cols="43" rows="5" readonly="readonly"></textarea>
        </td>
        <td></td>
    </tr>
    
</table>
</form>

<iframe name="hidden_frame" id="hidden_frame" style="display:none">
</iframe>

<script>
function checkXml() {
	document.getElementById("log").value = "loading......";
     var currentForm = document.getElementById("dataSubmit");
     var supplier = document.getElementById("supplier");
     currentForm.action = "/Windchill/netmarkets/jsp/ext/acae2/dms/processCheck.jsp?supplier="+supplier;
     currentForm.method = "post";
     currentForm.submit();
}

function checkFile() {
    var filePath = document.getElementById("uploadFile").value;
    var fileType = filePath.substring(filePath.lastIndexOf('.') + 1);
    
    if (fileType.toUpperCase() != "ZIP" && fileType.toUpperCase() != "RAR") {
        alert("${errorMessageType}");
        return;
    }
    else {
        document.getElementById("checkButton").disabled = false;
    }
}

function changeButton() {
	document.getElementById("submitButton").disabled = false;
}

function submitFile() {
	var currentForm = document.getElementById("dataSubmit");
	document.getElementById("log").value = "loading......";
	currentForm.action = "/Windchill/netmarkets/jsp/ext/acae2/dms/processSubmit.jsp";
    currentForm.method = "post";
    currentForm.submit();
}

function callback(msg) {
//     document.getElementById("uploadFile").outerHTML = document.getElementById("uploadFile").outerHTML;
    document.getElementById("log").value = msg;
}
</script>